<?php

class FeedController extends Controller
{
	private $_model;
	
	public $layout='column2';
	
	/**
	 *	默认执行动作
	 */
	public function actionFastSend()
	{
		//ajax传入
		if(Yii::app()->request->isAjaxRequest)
		{
			$_fastVal = trim(Yii::app()->request->getParam('_fastVal'));
			$_usereventval = Yii::app()->request->getParam('_usereventval');
			$_fastVal = CHtml::encode($_fastVal);
			if(empty($_fastVal))
			{
				echo CJSON::encode(array('_errorCode'=>'1000'));
				exit;
			}
			//装入数据库
			$model=new FeedList();
			$model->content = $_fastVal;
			$model->dayevent = !empty($_usereventval) ? '【'.$_usereventval.'】' : '';
			$model->user_id = Yii::app()->user->getId();
			$model->create_time = time();
			$model->save();
		}
		else
			echo '错误';
	}

	public function actionPhoto()
	{
		//ajax传入
		if(Yii::app()->request->isAjaxRequest)
		{
			$_fastVal = Yii::app()->request->getParam('_fastVal');
			$_fastVal = CHtml::encode($_fastVal);
			
			$_imgVal = Yii::app()->request->getParam('_imgVal');
			$_imgVal = CHtml::encode($_imgVal);
			
			$_usereventval = Yii::app()->request->getParam('_usereventval');
			$_usereventval = CHtml::encode($_usereventval);
			
			//装入数据库
			$model=new FeedList();
			$model->content = $_fastVal;
			$model->dayevent = !empty($_usereventval) ? '【'.$_usereventval.'】' : '';
			$model->type = 'photo';
			$model->pic = $_imgVal;
			$model->user_id = Yii::app()->user->getId();
			$model->create_time = time();
			$model->save();
		}
		else
			echo '错误';
	}
	
	/*
	 * @得到评价列表
	 */
	public function actionComments()
	{
		$feed = $this->loadModel();
		$_comments = $this->renderPartial('_comments',array('model'=>$feed),true);
		$_commentInfo['_html'] = $_comments;
		echo CJSON::encode($_commentInfo);
	} 
	/*
	 * @得到以主键为条件的MODEL
	 */
	public function loadModel()
	{
		if($this->_model===null)
		{
			if(isset($_GET['event_id']))
			{
				$this->_model=FeedList::model()->findByPk($_GET['event_id']);
			}
			if($this->_model===null)
				echo '非法错误';
		}
		return $this->_model;
	}

	/*
	 * @发表评论
	 */
	public function actionAddComment()
	{
		if(Yii::app()->request->isAjaxRequest)
		{
			$_contentval = Yii::app()->request->getParam('contentval');
			$_contentval = CHtml::encode($_contentval);

			$_event_id = Yii::app()->request->getParam('event_id');
			$_event_id = CHtml::encode($_event_id);
			
			$model=new FeedList();
			$model->content = $_contentval;
			$model->user_id = Yii::app()->user->getId();
			$model->type = 'comment';
			$model->create_time = time();
			$model->save();

			//增加评价数
			$model->updateCounters(array('commented_count'=>1),'event_id='.$_event_id);
			//插入关系表
			FeedRelation::model()->addRelation($model->attributes['event_id'],$_event_id, "comment");
		}
		else
			echo '非法错误';
		
	}
	
	/*
	 * @发表框
	 */
	public function actionShowFeedSendHTML()
	{
		//得到用户事件列表
		$date = date('Y-m-d');
		$criteria=new CDbCriteria;
		$criteria->condition='user_id=:user_id AND create_time = :create_time';
		$criteria->params=array(':user_id'=>Yii::app()->user->getId(),'create_time'=>$date);
		$_event_list = UserEvent::model()->findAll($criteria);	
		$this->renderPartial('_feedofsend',array('eventlist'=>$_event_list));
	}

	/*
	 * 上传图片
	 */
	public function actionUploadPhoto()
	{
        $upload = new UploadFile();
        //设置上传文件大小
        $upload->maxSize = 3292200;
        //设置上传文件类型
        $upload->allowExts = explode(',', 'jpg,gif,png,jpeg');
        $upload->savePath = 'uploads/';
        $upload->saveRule = $this->genUploadFileName();
        $upload->autoSub = true;
        
        //产生缩略图
        $upload->thumb = true;
        $upload->thumbPrefix = 'thumb_';
        $upload->thumbPath = 'uploads/thumb/';
        //设置缩略图最大宽度
        $upload->thumbMaxWidth = '200';
        //设置缩略图最大高度
        $upload->thumbMaxHeight = '125';

        //上传成功后的完整路径
        if (!$upload->upload())
        {
			$ajaxMsg = $upload->getErrorMsg();
        }
        else
        {
        	$uploadList = $upload->getUploadFileInfo();
			//$ajaxMsg = 'http://localhost/ren/'.$uploadList[0]['savepath'].$uploadList[0]['savename'];
			//返回缩略图
			 $ajaxMsg = 'http://localhost/ren/'.$uploadList[0]['thumbpath'];
			 //Func::mk_dir($ajaxMsg);
        }
        echo $ajaxMsg;
	}

	/*
	 * @生成上传之后的图片名称
	 */
	public function genUploadFileName() {
		return time().'_'.Func::random(4);
	}
}
